package com.yang.game;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.squareup.okhttp.Request;
import com.yang.game.model.Pet;
import com.yang.game.model.Weapon;
import com.yang.game.utils.OkHttpClientManager;


public class InfoActivity extends AppCompatActivity {

    private TextView showAccount;
    private TextView showPassword;

    private Button buyBtn, activityBtn;
    private ProgressBar attackBar;
    private ProgressBar speedBar;
    private ProgressBar defenceBar;

    private Pet pet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);
        Intent intent = getIntent();
        String account = intent.getStringExtra("account");
        String password = intent.getStringExtra("password");
        String ownerId = intent.getStringExtra("id");
        showAccount = findViewById(R.id.showAccount);
        showPassword = findViewById(R.id.showPassword);
        // 设置账号密码显示
        showAccount.setText(account);
        showPassword.setText(password);

        activityBtn = findViewById(R.id.getUserListBtn);
        buyBtn = findViewById(R.id.buyBtn);
        attackBar = findViewById(R.id.attackBar);
        speedBar = findViewById(R.id.speedBar);
        defenceBar = findViewById(R.id.defenceBar);

        initPetInfo(ownerId);

        buyBtn.setOnClickListener(v -> {
            Intent buyIntent = new Intent(InfoActivity.this, ShopActivity.class);
            startActivityForResult(buyIntent, 2000);
        });

        activityBtn.setOnClickListener(v -> {
            Intent activityIntent  = new Intent(InfoActivity.this, ListActivity.class);
            startActivity(activityIntent);
        });
    }

    private void initPetInfo(String ownerId) {
        // 查询宠物信息
        OkHttpClientManager.getAsyn("http://10.99.212.127:9090/api/pet/get/" + ownerId, new OkHttpClientManager.ResultCallback<Pet>() {
            @Override
            public void onError(Request request, Exception e) {
                Log.d("error", e.toString());
            }

            @Override
            public void onResponse(Pet response) {
                if (response == null) {
                    Toast.makeText(InfoActivity.this, "宠物信息获取失败", Toast.LENGTH_LONG).show();
                    return;
                }
                pet = response;
                Toast.makeText(InfoActivity.this, "宠物信息获取成功", Toast.LENGTH_LONG).show();
                // 设置基本信息
                speedBar.setProgress(pet.getSpeed());
                attackBar.setProgress(pet.getAttack());
                defenceBar.setProgress(pet.getDefence());
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Weapon weapon = (Weapon) data.getSerializableExtra("weapon");
//        // 更新宠物属性
        OkHttpClientManager.Param param1 = new OkHttpClientManager.Param("id", pet.getId());
        OkHttpClientManager.Param param2 = new OkHttpClientManager.Param("speed", weapon.getSpeed() + "");
        OkHttpClientManager.Param param3 = new OkHttpClientManager.Param("attack", weapon.getAttack() + "");
        OkHttpClientManager.Param param4 = new OkHttpClientManager.Param("defence", weapon.getDefence() + "");
        OkHttpClientManager.postAsyn("http://10.99.212.127:9090/api/pet/update", new OkHttpClientManager.ResultCallback<Pet>() {
            @Override
            public void onError(Request request, Exception e) {

            }
            @Override
            public void onResponse(Pet response) {
                if (response == null) {
                    Toast.makeText(InfoActivity.this, "更新失败", Toast.LENGTH_LONG).show();
                    return;
                }
                speedBar.setProgress(response.getSpeed());
                attackBar.setProgress(response.getAttack());
                defenceBar.setProgress(response.getDefence());
            }
        }, param1, param2, param3, param4);
    }
}
